import Vue from 'vue';
import { Module } from 'vuex';
import { API_URL, KEY_AUTH_TOKEN, LOGIN_STATUS } from '@/lib/constant';
import { RootState } from '@/types/store';
import { api } from '@/lib/api';
import { RawLocation } from 'vue-router';
import store from '.';
import { url } from '@/lib/utils';

export interface AuthState {
    /** 登录状态 */
    hasLogin: Auth.LoginStatus;
    /** 显示登陆弹窗 */
    showUserInfo: boolean;
    /** 显示绑定弹窗 */
    showBindMobile: boolean;
    /** 绑定手机号后跳转地址 */
    bindMobileFrom: RawLocation | null;
}

const m: Module<AuthState, RootState> = {
    namespaced: true,
    state: {
        /** 是否已经登录 */
        hasLogin: LOGIN_STATUS.LOGOUT,
        showUserInfo: false,
        showBindMobile: false,
        bindMobileFrom: null,
    },
    mutations: {
        setLogin(state, status) {
            state.hasLogin = status;
        },
        showUserInfo(state, showUserInfo) {
            state.showUserInfo = showUserInfo;
        },
        showBindMobile(state, showBindMobile) {
            state.showBindMobile = showBindMobile;
        },
        setBindMobileFrom(state, from) {
            state.bindMobileFrom = from;
        },
    },
    actions: {
        /**
         * 登录
         */
        async login(context, { code }) {
            const res = await api.post({
                url: `${API_URL}/api/auth/login`,
                data: {
                    code,
                },
            });
            const rdata = res.data;
            if (rdata.status === 0 && rdata.data) {
                // 成功
                await api.setStorage({
                    key: KEY_AUTH_TOKEN,
                    data: rdata.data.token,
                });
            }
            return res;
        },
        /**
         * 绑定手机号
         */
        async bindMobile({ state, commit, dispatch }, { encryptedData, iv }) {
            const res = await api.post({
                url: `${url}/api/encryptor`,
                data: {
                    encrypted_data: encryptedData,
                    iv,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const mobileData = await api.post({
                    url: `${url}/api/user/mobile`,
                    data: {
                        mobile: res.data.data.phoneNumber,
                    },
                });
                if (mobileData.statusCode === 200 && mobileData.data.status === 0) {
                    await store.dispatch('user/getBasicInfo', true);
                }
                // // 需要更新session
                // const sessionChanged = res.data.data.user_changed;
                // if (sessionChanged) {
                //     const session = res.header['Set-Cookie'];
                //     await api.setStorageSync({
                //         key: KEY_AUTH_TOKEN,
                //         data: session,
                //     });
                //     await Promise.all([
                //         dispatch('activity/getNewbieInfo', true, { root: true, }),
                //         dispatch('user/getBasicInfo', true),
                //     ]);
                //     wx.reLaunch({
                //         url: '/pages/index/main',
                //     });
                // } else {
                //     await dispatch('user/getBasicInfo', true);
                // }
            }
            return res;
        },
        // 获取微信解密手机号
        async getphoneNumberIv({ state, commit, dispatch }, { encryptedData, iv }) {
            const res = await api.post({
                url: `${url}/api/encryptor`,
                data: {
                    encrypted_data: encryptedData,
                    iv,
                },
            });
            
            return res;
        },
    },
};

export default m;
